<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.model.User"%>

<script type="text/javascript">
<!--
	$(document).ready(function() {		
		$("#cancelEditPaymentCanceledReasonBtn").click(function() {
			$("#editPaymentCanceledDiv").dialog("close");
		});
		
		$("[id^='doAjaxSavePaymentCanceledReasonBtn-']").click(function() {
			var trId = this.id.substring(35);
			doSavePaymentCanceledReasonAction(trId);			
		});
		
		$("[id^='doAjaxDeletePaymentCanceledReasonBtn-']").click(function() {
			var trId = this.id.substring(37);
			doDeletePaymentCanceledReasonAction(trId);			
		});
		
		$("#addEmptyPaymentCanceledReasonBtn").click(function() {
			addTableRow( $('.paymentCanceledReasonList'));
		});
		
	});

	function doSavePaymentCanceledReasonAction(trId)
	{
		var paymentCanceledReasonPkId = '#paymentCanceledReason\\.paymentCanceledReasonPk-' + trId;
		var paymentCanceledReasonPk = $(paymentCanceledReasonPkId).val();
		
		var canceledNoId = '#paymentCanceledReason\\.canceledNo-' + trId;
		var canceledNo = $(canceledNoId).val();
		
		var cancelReasonId = '#paymentCanceledReason\\.cancelReason-' + trId;
		var cancelReason = $(cancelReasonId).val();		
		
		var groupNo = $('#editGroup\\.groupNo').val();
		groupNo=groupNo.substring(0,8);
	
		var canceledNoRegExp = /[^a-zA-Z0-9]/;
		
		var errMsg = "";
		if (null == canceledNo || 0 == canceledNo.length){
			errMsg = errMsg + "銷帳代號是必要欄位<br/>";
		}		
	
		if(canceledNo.length > 0){
			
			if(canceledNoRegExp.test(canceledNo)){
				errMsg = errMsg + "銷帳代號必須是英、數字<br/>";
			}
			if ( 3 < canceledNo.length){
				errMsg = errMsg + "銷帳代號長度最多只能輸入3碼英、數字<br/>";
			}
		}
		
		
		if (null == cancelReason || 0 == cancelReason.length){
			errMsg = errMsg + "銷帳原因是必要欄位<br/>";
		}else{			
			if ( 20 < cancelReason.length){
				errMsg = errMsg + "銷帳原因長度最多只能輸入20個中文字<br/>";
			}	
		}	
		
		
		if (0 != errMsg.length){
			$("#ajaxErrorMessageDiv").html(errMsg);
			$("#ajaxErrorMessageDiv" ).dialog({  
				title:'請修正下列錯誤' 
				,width: 300
				, resizable: false
				, modal: true
				, buttons: {
					Ok: function() {
						$( this ).dialog( "close" );
					}
				}
			});
			return false;
		}
		
		$('#hidden\\.paymentCanceledReason\\.paymentCanceledReasonPk').val(paymentCanceledReasonPk);
		$('#hidden\\.paymentCanceledReason\\.canceledNo').val(canceledNo);
		$('#hidden\\.paymentCanceledReason\\.cancelReason').val(cancelReason);
		$('#doAjaxMethodx').attr('name','doAjaxSavePaymentCanceledReason');
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9007.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editPaymentCanceledReasonHiddenForm").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#editPaymentCanceledDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							if (1==$("#doAjaxSaveResultPaymentCancelCode").val()){								
								toAjaxEditPaymentCanceledAction($("#hidden\\.paymentCanceledReason\\.group\\.groupPk").val());
							}
							$(this).dialog("close");
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});
	}
	
	function doDeletePaymentCanceledReasonAction(trId)
	{
		var paymentCanceledReasonPkId = '#paymentCanceledReason\\.paymentCanceledReasonPk-' + trId;
		var paymentCanceledReasonPk = $(paymentCanceledReasonPkId).val();
		
		$('#hidden\\.paymentCanceledReason\\.paymentCanceledReasonPk').val(paymentCanceledReasonPk);
		$('#doAjaxMethodx').attr('name','doAjaxDeletePaymentCanceledReason');
		
		waitingDialog({});
		$.ajax({
			url : "<c:url value='/ajax/Soez9007.action'/>",
			async : true,
			type : 'POST',
			data : 	$("#editPaymentCanceledReasonHiddenForm").serialize(),
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest) {
				$("#editPaymentCanceledDiv").dialog("destroy");
				$("#ajaxErrorMessageDiv").html(data);
				$("#ajaxErrorMessageDiv").dialog({
					title : '<%=UserUtil.getCurrentMenuItemName() %>',
					width : 300,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {								
							if (1==$("#doAjaxDeleteResultPaymentCancelCode").val()){
								toAjaxEditPaymentCanceledAction($("#hidden\\.paymentCanceledReason\\.group\\.groupPk").val());
							}
							$(this).dialog("close");
						}
					}
				});
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv").dialog({
					title : '系統錯誤',
					width : 600,
					resizable : false,
					modal : true,
					buttons : {
						Ok : function() {
							$(this).dialog("close");
						}
					}
				});
			},
			complete : function(XMLHttpRequest, textStatus) {
				closeWaitingDialog();
			}
		});
	}
	
	function addTableRow(jQtable){

	    jQtable.each(function(){
	        var $table = $(this);
	        var newRowNum = $('tbody tr', this).length;
	        // Number of td's in the last table row
	        var n = $('tr:last td', this).length;
	        var tds = '<tr id="tr-'+newRowNum+'">';
	        	        
	        tds += '<td>';
	        tds += '<input id="paymentCanceledReason.paymentCanceledReasonPk-'+newRowNum+'" name="paymentCanceledReason.paymentCanceledReasonPk" value="" type="hidden">';
	        tds += '<input id="paymentCanceledReason.canceledNo-'+newRowNum+'" name="paymentCanceledReason.canceledNo" value="" maxlength="20" type="text">';
	        tds += '</td>';
	        
	        tds += '<td>';
	        tds += '<input id="paymentCanceledReason.cancelReason-'+newRowNum+'" name="paymentCanceledReason.cancelReason" value="" maxlength="200" type="text">';
	        tds += '</td>';

	        tds += '<td>';
	        tds += '<input id="doAjaxSavePaymentCanceledReasonBtn-'+newRowNum+'" value="確認" type="button" onclick="doSavePaymentCanceledReasonAction('+newRowNum+')" >&nbsp;';
	        tds += '<input id="doAjaxDeletePaymentCanceledReasonBtn-'+newRowNum+'" value="刪除" type="button" onclick="doDeletePaymentCanceledReasonAction('+newRowNum+')" disabled="disabled">';	        
	        tds += '</td>';
	        
	        tds += '</tr>';
	        
	        if($('tbody', this).length > 0){
	            $('tbody', this).append(tds);
	        }else {
	            $(this).append(tds);
	        }
	    });
	}
//-->
</script>
<div id="editPaymentCanceledReasonFormDiv">	
	<table id="listTable" class="paymentCanceledReasonList">
		<thead>
			<tr>
				<th>銷帳代碼</th>
				<th>原因</th>				
				<th>&nbsp;</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach items="${actionBean.queryPaymentCanceledReason.paginatedList}" var="viewObj" varStatus="rowstat">
			<tr id="tr-${rowstat.index}">
				<td>
					<input id="paymentCanceledReason.paymentCanceledReasonPk-<c:out value='${rowstat.index}'/>" name="paymentCanceledReason.paymentCanceledReasonPk" type="hidden" value="<c:out value="${viewObj.paymentCanceledReasonPk}"/>" />
					<input id="paymentCanceledReason.canceledNo-<c:out value='${rowstat.index}'/>" name="paymentCanceledReason.canceledNo" type="text" value="<c:out value="${viewObj.canceledNo}"/>" maxlength="3"  length="3"/>
				</td>
				<td>
					<input id="paymentCanceledReason.cancelReason-<c:out value='${rowstat.index}'/>" name="paymentCanceledReason.cancelReason" type="text" value="<c:out value="${viewObj.cancelReason}"/>" maxlength="20" length="20"/>
				</td>						
				<td>
					<input type="button" value="確認" id="doAjaxSavePaymentCanceledReasonBtn-<c:out value='${rowstat.index}'/>"/>&nbsp;<input type="button" value="刪除" id="doAjaxDeletePaymentCanceledReasonBtn-<c:out value='${rowstat.index}'/>"/>
				</td>					
			</tr>
			</c:forEach>
		</tbody>
		<tfoot>			
			<tr>
				<td colspan="4" align="center">
					<input type="button" value="新增" id="addEmptyPaymentCanceledReasonBtn"/>
					<input type="button" value="離開" id="cancelEditPaymentCanceledReasonBtn"/>
				</td>				
			</tr>
		</tfoot>
	</table>
</div>

<div id="editPaymentCanceledReasonHiddenDiv" style="display: none;">
<stripes:form id="editPaymentCanceledReasonHiddenForm" name="editPaymentCanceledReasonHiddenForm" 
	beanclass="com.feib.soeasy.action.Soez9007ActionBean" method="post" onsubmit="javascript: return false;">
	<input id="hidden.paymentCanceledReason.group.groupPk" name="paymentCanceledReason.group.groupPk" value="${actionBean.editGroup.groupPk}"/>
	<input id="hidden.paymentCanceledReason.paymentCanceledReasonPk" name="paymentCanceledReason.paymentCanceledReasonPk" />
	<input id="hidden.paymentCanceledReason.canceledNo" name="paymentCanceledReason.canceledNo" />
	<input id="hidden.paymentCanceledReason.cancelReason" name="paymentCanceledReason.cancelReason" />	
	
	<input type="hidden" id="doAjaxMethodx" name="doAjaxSavePaymentCanceledReason" />
</stripes:form>
</div>

